package com.maxatime.web.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.maxatime.common.constant.BusinessContents;
import com.maxatime.exception.PenguinException;
import com.maxatime.pojo.entity.StudentBasicInfo;
import com.maxatime.pojo.entity.StudentRegister;
import com.maxatime.pojo.entity.Weather;
import com.maxatime.service.LoginService;
import com.maxatime.service.StudentBasicInfoService;
import com.maxatime.service.WeatherService;
import com.maxatime.util.MyContent;
import com.maxatime.util.PasswordManagerUtil;
import com.maxatime.util.WeatherContent;

@Controller
@RequestMapping("/Login")//天气
public class loginController {
	
	@Autowired
	LoginService loginService;
	
	@Autowired
	WeatherService weatherService;
	
	@Autowired
	StudentBasicInfoService studentBasicInfoService;
	
	Logger log = LogManager.getLogger();
	
	@RequestMapping("/loginpage")
	public String loginpage() {
		return "logon/login";
	}
	@RequestMapping("/student")
	public String student() {
		return "logon/xq";
	}
	
	@RequestMapping("/login")
	@ResponseBody
	public Map<String, Object> login(@RequestBody String JsonStr,HttpSession session){
		JSONObject json = JSONObject.parseObject(JsonStr);
	
		String email = json.getString("email");
		String password = json.getString("password");
		StudentRegister studentRegister = new StudentRegister();
		
		studentRegister.setEmail(email);
		studentRegister.setPassword(PasswordManagerUtil.getMd5DigestAsHex(password));//密码加密
		StudentRegister stu=loginService.map(email, password);
		
		Map<String, Object> m = new HashMap<>();
		if(stu!=null) {
			m.put(MyContent.CODE, "0000");
			m.put(MyContent.MSG, "登录成功");
			//数据保存到SESSION
			session.setAttribute(BusinessContents.STUDENT_ID, stu.getStudentId());
			
		}else if(stu==null){
			m.put(MyContent.CODE, "2222");
			m.put(MyContent.MSG, "账号或密码错误，请重新输入");
		}
		return m;
	}
//	@RequestMapping("/quest")
//	@ResponseBody
//	public Map<String, Object> quest(@RequestBody StudentRegister studentRegister){
//		List<StudentRegister> list = loginService.questData(studentRegister);
//		Map<String, Object> m = new HashMap<>();
//		m.put(MyContent.CODE, "0000");
//		m.put(MyContent.MSG, "查询成功");
//		m.put(MyContent.DATA, list);
//		return m;
//	}
	@RequestMapping("/cxstudent")
	@ResponseBody
	public Map<String, Object > cxstudent(@RequestBody String JsonStr,HttpServletRequest request){
		Map<String, Object > m = new HashMap<>();
		//session的studentId值取出来
		Object studentId = request.getSession().getAttribute(BusinessContents.STUDENT_ID);
		//类型转换Object转换为String
		String studentId1 = studentId.toString();
		List<StudentBasicInfo> student = studentBasicInfoService.finddata(studentId1);
		
		System.out.println("stundet!!!!"+student);
		m.put(MyContent.CODE, "3333");
		m.put(MyContent.MSG, "成功");
		m.put(MyContent.DATA, student);
		return m;
	}
	
	//经纬度转城市
	@RequestMapping("/getWeatherByLongitudeAndLatitude")
	@ResponseBody
	public Map<String, Object> getWeatherByLongitudeAndLatitude(@RequestParam("longitude") String longitude,
			@RequestParam("latitude") String latitude) {

		// 返回值
		Map<String, Object> m = new HashMap<String, Object>();
		log.info("getWeatherByLongitudeAndLatitude请求参数-精度 " + longitude + " 维度: " + latitude);

		if(StringUtils.trimToNull(longitude) == null && StringUtils.trimToNull(latitude) == null) {
			m.put(BusinessContents.CODE, "9999");
			m.put(BusinessContents.MSG, "经纬度为空");
			return m;
		}

		// TODO 调用查询天气服务
		String cityName = "";
		try {
			log.info("weatherService.getCityNameByLongitudeAndLatitude-req-经度::: " + longitude + " 维度::: " + latitude);
			cityName = weatherService.getCityNameByLongitudeAndLatitude(longitude, latitude);
			log.info("weatherService.getCityNameByLongitudeAndLatitude-resp::: " + cityName);
		} catch(PenguinException pe) {
			m.put(BusinessContents.CODE, "9999");
			m.put(BusinessContents.MSG, pe.getMessage());
		} catch(Exception e) {
			m.put(BusinessContents.CODE, "9999");
			m.put(BusinessContents.MSG, e.getMessage());
		}

		m.put(BusinessContents.CODE, "0000");
		m.put(BusinessContents.MSG, "交易成功");
		Map<String, Object> mapCity  = new HashMap<String, Object>();
		mapCity.put("city", cityName);
		m.put("data", mapCity);
		log.info("-resp::: " + m);
		return m;
	}
	//根据城市查天气
	@RequestMapping("/getWeatherByCity")
	@ResponseBody
	public Map<String, Object> getWeatherByCity(@RequestParam("city") String city) {
		// 返回值
		Map<String, Object> m = new HashMap<String, Object>();
		log.info("getWeatherByCity请求参数: " + city);

		if(StringUtils.trimToNull(city) == null) {
			m.put(BusinessContents.CODE, "9999");
			m.put(BusinessContents.MSG, "城市名称错误");
			return m;
		}

		// TODO 调用查询天气服务
		Map<String, Object> resultMap = null;
		try {
			log.info("weatherService.getWeatherByCityName-req::: " + city);
			resultMap = weatherService.getWeatherByCityName(city);
			log.info("weatherService.getWeatherByCityName-resp::: " + resultMap);
		} catch(PenguinException pe) {
			m.put(BusinessContents.CODE, "9999");
			m.put(BusinessContents.MSG, pe.getMessage());
		} catch(Exception e) {
			m.put(BusinessContents.CODE, "9999");
			m.put(BusinessContents.MSG, e.getMessage());
		}

		m.put(BusinessContents.CODE, "0000");
		m.put(BusinessContents.MSG, "交易成功");
		m.put("data", resultMap.get(WeatherContent.WEATHER_RESULT_DATA));
		log.info("getWeatherByCity-resp::: " + m);
		return m;
	}
	
}
